package com.hieu.tripmanager;

import java.util.ArrayList;

import com.hieu.sql.DAL;
import com.hieu.util.Util;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SettingActivity extends Activity {
	private TextView txtSumBudget;
	private EditText edtTransport;
	private EditText edtFood;
	private EditText edtRent;
	private EditText edtFuel;
	private EditText edtLodging;
	private EditText edtPhone;
	private EditText edtShop;
	private EditText edtRefresh;
	private EditText edtGroceries;
	private EditText edtOther;
	private Button btnSave;
	private DAL mDAL;
	private ArrayList<Long> mList;
	long sum = 0;
	private Util util = new Util();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// Kiểm tra có chuyến đi đang thực hiện không
				mDAL = new DAL(this);
				mList = new ArrayList<Long>();
				
				boolean check = util.checkIsTrip(SettingActivity.this);
				if (check) {
					setContentView(R.layout.setting_activity_layout);

					txtSumBudget = (TextView) findViewById(R.id.txtTotalBudget);
					edtTransport = (EditText) findViewById(R.id.edtTransport);
					edtShop = (EditText) findViewById(R.id.edtShopping);
					edtRent = (EditText) findViewById(R.id.edtRent);
					edtRefresh = (EditText) findViewById(R.id.edtRefresh);
					edtPhone = (EditText) findViewById(R.id.edtPhone);
					edtOther = (EditText) findViewById(R.id.edtOther);
					edtLodging = (EditText) findViewById(R.id.edtLodging);
					edtGroceries = (EditText) findViewById(R.id.edtGroceries);
					edtFuel = (EditText) findViewById(R.id.edtFuel);
					edtFood = (EditText) findViewById(R.id.edtFood);
					btnSave = (Button) findViewById(R.id.btnSaveSetting);

					loadData();
					
					btnSave.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							boolean empty = false;
							boolean special = false;
							// TODO Auto-generated method stub
							ArrayList<Long> list = new ArrayList<Long>();
							ArrayList<String> listInput = new ArrayList<String>();
							String trans = String.valueOf(edtTransport.getText());
							String food = String.valueOf(edtFood.getText());
							String lodging = String.valueOf(edtLodging.getText());
							String rent = String.valueOf(edtRent.getText());
							String fuel = String.valueOf(edtFuel.getText());
							String phone = String.valueOf(edtPhone.getText());
							String shop = String.valueOf(edtShop.getText());
							String refresh = String.valueOf(edtRefresh.getText());
							String groceries = String.valueOf(edtGroceries.getText());
							String other = String.valueOf(edtOther.getText()); 
							listInput.add(trans);
							listInput.add(food);
							listInput.add(lodging);
							listInput.add(rent);
							listInput.add(fuel);
							listInput.add(phone);
							listInput.add(shop);
							listInput.add(refresh);
							listInput.add(groceries);
							listInput.add(other);
							for (int i = 0; i < listInput.size(); i++){
								if(util.checkIsEmpty(listInput.get(i))) empty = true;
								if(util.checkValiateNumber(listInput.get(i))) special = true;
								Log.v(""+listInput.get(i), ""+empty+"---"+special);
							}
							if(!empty){
								if(!special){
									for(int i = 0; i < listInput.size(); i++){
										list.add(Long.valueOf(listInput.get(i)));
									}
									if (mDAL != null) {
											mDAL.updateBudget(list);

									}
									loadData();
									Toast.makeText(v.getContext(), "Cập nhật thành công", 2000).show();
																
								} else {
									Toast.makeText(v.getContext(), "Các mục cập nhật có chứa kí tự đặc biệt", 2000).show();
								}
							} else {
								Toast.makeText(v.getContext(), "Nhập thiếu ngân sách các mục", 2000).show();
							}

						}
					});

				} else {
					setContentView(R.layout.layout);
					Toast.makeText(getBaseContext(),
							"Hiện tại không có chuyến đi mới!!!", Toast.LENGTH_SHORT).show();
				}

	}

	private void loadData() {
		if (mDAL != null) {
			mList = mDAL.getBudgetList();
			sum = mDAL.getSumBudget();
		}

		if (mList != null) {
			txtSumBudget.setText(util.convertMoney(sum));
			edtTransport.setText(util.convertMoney(mList.get(0)));
			edtFood.setText(util.convertMoney(mList.get(1)));
			edtLodging.setText(util.convertMoney(mList.get(2)));
			edtRent.setText(util.convertMoney(mList.get(3)));
			edtFuel.setText(util.convertMoney(mList.get(4)));
			edtPhone.setText(util.convertMoney(mList.get(5)));
			edtShop.setText(util.convertMoney(mList.get(6)));
			edtRefresh.setText(util.convertMoney(mList.get(7)));
			edtGroceries.setText(util.convertMoney(mList.get(8)));
			edtOther.setText(util.convertMoney(mList.get(9)));
			// edtFood.setText("100");
			// edtLodging.setText("100");
			// edtRent.setText("100");
			// edtFuel.setText("100");
			// edtPhone.setText("100");
			// edtShop.setText("100");
			// edtRefresh.setText("100");
			// edtGroceries.setText("100");
			// edtOther.setText("100");
		}
	}
}
